var app = getApp(); 
Page({
  data: {
    message:'',
    jsonresult:'',
    user:{},
    topics: [],
    topics_length: 0,
    topicId:'',
  },

  onLoad: function () {
    console.log('onLoad')
    this.setData({
      user: app.globalData.user
    })
    this.getTopics();
  },

  radioChange: function (e) {
    this.setData({
      topicId: e.detail.value,
      message: ""
    })
  },

  public:function(e){
    if (this.data.topicId == ""){
      this.setData({
        message:"请先选择问题~"
      })
    }else{
      console.log(this.data.userId)
      console.log(this.data.typeId)
      var that = this
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/approve_IsApprove.action', 
        data: {
          'topic.topicId': this.data.topicId,
        },
        header: { 
          "content-type": "application/x-www-form-urlencoded"
        }, 
        method: "POST",
        success: function (res) {
          let jsonresult =JSON.parse(res.data)
          console.log(jsonresult.result)
          if (jsonresult.result == "error"){
             that.setData({
               message: jsonresult.msg 
            })
          }else{
            wx.navigateTo({
              url: '../contributeNext/contributeNext?topicId=' + that.data.topicId
            })
          }
        },
        fail: function (err) {
          console.log(err)
        }
      })
    }
  },


  getTopics: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/topic_findAllTopicsByUser.action',
      data: {
        'user.userId': this.data.user.userId,
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          that.setData({
            topics: [],
            topics_length: 0
          })
        } else {
          that.setData({
            topics: jsonresult.topics,
            topics_length: jsonresult.topics.length
          })
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })

  },
})